package com.wudi.estate.controller.homemanage;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.homemanage.Home;
import com.wudi.estate.bean.homemanage.Homes;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.homemanage.BuildingsService;
import com.wudi.estate.service.homemanage.HomeService;
import com.wudi.estate.service.homemanage.HomesService;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.*;

@RequestMapping("/homes")
@Controller
public class HomesController {
    @Autowired
    private HomesService homesService;
    @Autowired
    private HomeService homeService;
    @Autowired
    private BuildingsService buildingsService;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private CommunityService communityService;

    @RequestMapping("/listUi")
    public String homes_listUi(ModelMap map) {
        List<Map<String, Object>> homesAndBAndHAndPer = new ArrayList<>();
        List<Homes> homesList = new ArrayList<>();

        try {
            homesList = homesService.selectList();
            for (Homes h : homesList) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("homes", h);
                Home home = homeService.selectById(h.getHid());
                map1.put("home", home);
                Personnel personnel = personnelService.selectById(h.getPid());
                map1.put("personnel", personnel);
                Buildings buildings = buildingsService.selectById(personnel.getBid());
                map1.put("buildings", buildings);
                Community community = communityService.selectById(buildings.getCid());
                map1.put("community", community);
                homesAndBAndHAndPer.add(map1);
            }
            map.addAttribute("homesAndBAndHAndPer", homesAndBAndHAndPer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "homes_list";
    }

    @RequestMapping("/addUi")
    public String homes_addUi(ModelMap map){
        List<Home>  home = new ArrayList<>();
        List<Buildings> buildingsList = new ArrayList<>();
        List<Community> communities = new ArrayList<>();
        List<Personnel> personnels = new ArrayList<>();
        try {
            buildingsList=buildingsService.selectAll();
            home=homeService.selectList();
            communities= communityService.select();
            personnels=personnelService.selectAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("communities",communities);
        map.addAttribute("buildingsList",buildingsList);
        map.addAttribute("home",home);
        map.addAttribute("personnels",personnels);
        return "homes_add";
    }
    @RequestMapping("/add")
    public String homes_add(Homes homes,String hname ,ModelMap map){
        try {
            homeService.insertName(hname);
            Home home = homeService.selectByName(hname);
            homes.setHid(home.getId());
            homesService.insert(homes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/homes/listUi";
    }
    @RequestMapping("/editUi")
    public String homes_editUi(long id,ModelMap map){
        Homes homes = new Homes();
        Home home1 = new Home();
        List<Buildings> buildingsList = new ArrayList<>();
        List<Personnel> personnels = new ArrayList<>();

        try {
            personnels=personnelService.selectAll();
            buildingsList=buildingsService.selectAll();
            homes = homesService.selectById(id);
            home1 = homeService.selectById(homes.getHid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("buildingsList",buildingsList);
        map.addAttribute("personnels",personnels);
        map.addAttribute("home1",home1);
        map.addAttribute("homes",homes);
        return "homes_edit";
    }
    @RequestMapping("/edit")
    public String homes_add(long id,String hname,Homes homes){
        System.out.println(homes);
        try {
            Homes homes1 = homesService.selectById(id);
            Home home = new Home();
            home = homeService.selectById(homes1.getHid());
                homeService.updateById(homes1.getHid(),hname);

            homes.setHid(home.getId());
            homes.setHousingTime(new Date());
            homesService.update(homes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/homes/listUi";
    }
}
